//
//  EmojiView.m
//  CustomEmojiKeyBoardDemo
//
//  Created by Lisa on 16/11/9.
//  Copyright © 2016年 Elisa. All rights reserved.
//

#import "EmojiView.h"
#import "EmojiCollectionViewCell.h"

#define kButtonWidth 45
#define kButtonHeight 25


@interface EmojiView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UICollectionView *emojiCollectionView;//展示emoji的view
@property (nonatomic, strong) NSMutableArray *mutEmojiArray;



@property (nonatomic, strong) UIButton *sendButton;
@property (nonatomic, strong) UIButton *deleteButton;

@end


@implementation EmojiView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
//        [self obtainDataFromPlist];
        [self creatSubViewsWithFrame:frame];
        self.backgroundColor = kBGColor;

    }
    return self;
}
- (id)initWithFrame:(CGRect)frame isCommuntity:(BOOL)isCommuntity
{
    if (self = [super initWithFrame:frame]) {
        [self obtainDataFromPlistWithCommuntity:isCommuntity];
        
        [self creatSubViewsWithFrame:frame];
        self.backgroundColor = kBGColor;
    }
    return self;
}


- (void)creatSubViewsWithFrame:(CGRect)frame
{
    //宽度 －－320－35 －－ 6-375 －37，6p－414-－
    CGFloat height = 35;

    if (kScreenBoundWidth == 375) {
        height = 37;
        
    }else if(kScreenBoundWidth == 414){
        height = 41;
    }
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setItemSize:CGSizeMake(height , height)];
    flowLayout.minimumLineSpacing = 7;
    flowLayout.minimumInteritemSpacing = 7;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.emojiCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(12, 0, frame.size.width - 25, frame.size.height - 35) collectionViewLayout:flowLayout];
    
    [self.emojiCollectionView registerClass:[EmojiCollectionViewCell class] forCellWithReuseIdentifier:kEmojiCollectionViewCellID];
    self.emojiCollectionView.delegate = self;
    self.emojiCollectionView.dataSource = self;
    self.emojiCollectionView.pagingEnabled = YES;
    self.emojiCollectionView.showsHorizontalScrollIndicator = NO;
    self.emojiCollectionView.backgroundColor = kBGColor;
    
    [self addSubview:self.emojiCollectionView];
    
    
    
    self.sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.sendButton.frame = CGRectMake(frame.size.width - 45-25, frame.size.height - 32, kButtonWidth+5, kButtonHeight);
    [self.sendButton setTitle:@"发 送" forState:UIControlStateNormal];
    self.sendButton.backgroundColor = kBGColor;
    [self.sendButton setTitleColor:kNavigationColor forState:UIControlStateNormal];
    self.sendButton.titleLabel.font = [UIFont systemFontOfSize:21];
    [self.sendButton addTarget:self action:@selector(sendButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.sendButton];
    self.deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.deleteButton.frame = CGRectMake(frame.size.width - 25 - 90 - 10, frame.size.height - 32, kButtonWidth, kButtonHeight);
    [self.deleteButton setBackgroundImage:[UIImage imageNamed:@"emojiDeleteBg"] forState:UIControlStateNormal];
    [self.deleteButton setBackgroundImage:[UIImage imageNamed:@"emojiDeleteBg"] forState:UIControlStateHighlighted];
    [self.deleteButton addTarget:self action:@selector(deletaButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:self.deleteButton];
//    /**
//     小圆点
//     */
//    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, frame.size.height - 32,frame.size.width/2 - 10, 20)];
//    self.pageControl.center = CGPointMake(self.center.x, frame.size.height - 15);
//    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
//    self.pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
//    [self addSubview:self.pageControl];
    //显示的行数
//    CGFloat baseHeight = height + 7;
//    if (IS_IPHONE4) {
//        baseHeight = height + 3;
//    }
//    NSInteger numCount = floor(self.emojiCollectionView.height/baseHeight);
//    self.pageControl.numberOfPages = ceilf(self.mutEmojiArray.count/(8.0 * numCount));
//    self.pageControl.currentPage = 0;
//    [self.pageControl addTarget:self action:@selector(pageAction) forControlEvents:UIControlEventTouchUpInside];

    
}

- (void)obtainDataFromPlistWithCommuntity:(BOOL)isCommuntity
{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"EmojisList" ofType:@"plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    self.mutEmojiArray = [NSMutableArray array];
    if(isCommuntity){
        self.mutEmojiArray = data[@"Communttity"];
    }else{
        self.mutEmojiArray = data[@"People"];
 
    }
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.mutEmojiArray.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    EmojiCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kEmojiCollectionViewCellID forIndexPath:indexPath];
    [cell updateEmojiCellWithStr:self.mutEmojiArray[indexPath.row]];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(emojiView:selectStr:)]) {
        [self.delegate emojiView:self selectStr:self.mutEmojiArray[indexPath.row]];
    }
}
//翻页后对分页控制器进行更新
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat contenOffset = scrollView.contentOffset.x;
    int page = contenOffset/scrollView.frame.size.width+((int)contenOffset%(int)scrollView.frame.size.width==0?0:1);
    self.pageControl.currentPage = page;
    
}
-(void)pageAction{
    NSInteger page = self.pageControl.currentPage;
    [self.emojiCollectionView setContentOffset:CGPointMake(kScreenBoundWidth * (page+1), 0)];
}

- (void)sendButtonAction:(UIButton *)button
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(emojiViewDidSend:)]) {
        [self.delegate emojiViewDidSend:self];
    }
}
- (void)deletaButtonAction:(UIButton *)button
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(emojiViewDidDelete:)]) {
        [self.delegate emojiViewDidDelete:self];
    }
}
#pragma mark - SETTERS
- (void)setSendStr:(NSString *)sendStr
{
    _sendStr = sendStr;
    if (sendStr) {
        [self.sendButton setTitle:sendStr forState:UIControlStateNormal];
    }
}
@end
